//
//  LoginViewController.m
//  WeChat
//
//  Created by wangcarl on 16/8/29.
//  Copyright © 2016年 wangcarl. All rights reserved.
//

#import "LoginViewController.h"
#import "AppDelegate.h"
#import "MBProgressHUD+HM.h"
#import "Account.h"

@interface LoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *userTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordText;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if ([Account shareAccount].isLogin) {
        self.userTextField.text=[Account shareAccount].user;
        self.passwordText.text=[Account shareAccount].pwd;

    }
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)loginBTN:(UIButton *)sender {
    if (self.userTextField.text.length==0||self.passwordText.text.length==0) {
        NSLog(@"user and pwd");
    }
    
    
    [MBProgressHUD showMessage:@"正在登录中。。。。。。"];
    
//  NSUserDefaults *defaults=  [NSUserDefaults standardUserDefaults] ;
//    [defaults setObject:self.userTextField.text forKey:@"user"];
//    [defaults setObject:self.passwordText.text forKey:@"pwd"];
//    [defaults synchronize];
    [Account shareAccount].user=self.userTextField.text ;
    [Account shareAccount].pwd=self.passwordText.text;
    
    __weak typeof(self) selfVc=self;
    
    [XMPPTool sharedXMPPTool].registerOperation=NO;
    [[XMPPTool sharedXMPPTool] xmppLogin:^(XMPPResultType resultType){
       
        [selfVc handleXMPPResultType:resultType];
    }];
              
}
-(void)handleXMPPResultType:(XMPPResultType)resultType
{
    dispatch_async(dispatch_get_main_queue(), ^{
        
        [MBProgressHUD hideHUD];
        if (resultType==XMPPResultTypeLoginSucess) {
            NSLog(@"%s",__func__);
            [self changeToMain];
            [Account shareAccount].login=YES;
            [[Account shareAccount] saveToSandBox];
        
        }else
        {
            [MBProgressHUD showError:@"用户名或密码错误"];
            NSLog(@"genglushibai,%s",__func__);
        }
        
        
    });

}
-(void)changeToMain
{
    id vc=  [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateInitialViewController];
    [UIApplication sharedApplication].keyWindow.rootViewController=vc;
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
